/*
 * Copyright 2016 The Cartographer Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CARTOGRAPHER_MAPPING_2D_SUBMAP_2D_H_
#define CARTOGRAPHER_MAPPING_2D_SUBMAP_2D_H_

#include <memory>
#include <vector>

#include "Eigen/Core"
#include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/mapping/2d/grid_2d.h"
#include "cartographer/mapping/2d/map_limits.h"
#include "cartographer/mapping/proto/serialization.pb.h"
#include "cartographer/mapping/proto/submap_visualization.pb.h"
#include "cartographer/mapping/proto/submaps_options_2d.pb.h"
#include "cartographer/mapping/range_data_inserter_interface.h"
#include "cartographer/mapping/submaps.h"
#include "cartographer/mapping/trajectory_node.h"
#include "cartographer/mapping/value_conversion_tables.h"
#include "cartographer/sensor/range_data.h"
#include "cartographer/transform/rigid_transform.h"

namespace cartographer {
	namespace mapping {

		proto::SubmapsOptions2D CreateSubmapsOptions2D(
			common::LuaParameterDictionary* parameter_dictionary);
		/*
		* 该类只是一个封装， 子图的详细信息，比如概率栅格的占用情况，子图的大小等都是在grid_的对象中记录。
		* 无论是封装子图的类Submap2D还是描述占用栅格的Grid2D都没有直接提供把激光雷达的扫描数据插入子图的方法，
		* 而是通过ActiveSubmaps2D所提供的插入器对象range_data_inserter_完成的，这是一种类似于配件的设计模式，
		* 如果我们有很多种插入方法，可以分别以插入器的形式实现它们，在ActiveSubmaps2D中根据需要具例化对象，
		如此我们就能够随意的切换算法， 不需要对其他模块作出过多的改动。
		*/
		class Submap2D : public Submap {
		public:
			Submap2D(const Eigen::Vector2f& origin, std::unique_ptr<Grid2D> grid,
				ValueConversionTables* conversion_tables);
			explicit Submap2D(const proto::Submap2D& proto,
				ValueConversionTables* conversion_tables);

			proto::Submap ToProto(bool include_grid_data) const override;
			void UpdateFromProto(const proto::Submap& proto) override;

			void ToResponseProto(const transform::Rigid3d& global_submap_pose,
				proto::SubmapQuery::Response* response) const override;

			const Grid2D* grid() const { return grid_.get(); }

			// Insert 'range_data' into this submap using 'range_data_inserter'. The
			// submap must not be finished yet.
			void InsertRangeData(const sensor::RangeData& range_data,
				const RangeDataInserterInterface* range_data_inserter);
			void Finish();

		private:
			std::unique_ptr<Grid2D> grid_;//ProbabilityGrid
			ValueConversionTables* conversion_tables_;
		};

		// The first active submap will be created on the insertion of the first range
		// data. Except during this initialization when no or only one single submap
		// exists, there are always two submaps into which range data is inserted: an
		// old submap that is used for matching, and a new one, which will be used for
		// matching next, that is being initialized.
		//
		// Once a certain number of range data have been inserted, the new submap is
		// considered initialized: the old submap is no longer changed, the "new" submap
		// is now the "old" submap and is used for scan-to-map matching. Moreover, a
		// "new" submap gets created. The "old" submap is forgotten by this object.
		class ActiveSubmaps2D {
		public:
			explicit ActiveSubmaps2D(const proto::SubmapsOptions2D& options);

			ActiveSubmaps2D(const ActiveSubmaps2D&) = delete;
			ActiveSubmaps2D& operator=(const ActiveSubmaps2D&) = delete;

			// Inserts 'range_data' into the Submap collection.
			std::vector<std::shared_ptr<const Submap2D>> InsertRangeData(
				const sensor::RangeData& range_data);

			std::vector<std::shared_ptr<const Submap2D>> submaps() const;

		private:
			std::unique_ptr<RangeDataInserterInterface> CreateRangeDataInserter();
			std::unique_ptr<GridInterface> CreateGrid(const Eigen::Vector2f& origin);
			void FinishSubmap();//完成新旧图的切换
			void AddSubmap(const Eigen::Vector2f& origin);

			const proto::SubmapsOptions2D options_;
			std::vector<std::shared_ptr<Submap2D>> submaps_;//保存当前维护子图的容器
			std::unique_ptr<RangeDataInserterInterface> range_data_inserter_;//用于将扫描数据插入子图的工具，我们称它为插入器
			ValueConversionTables conversion_tables_;
		};

	}  // namespace mapping
}  // namespace cartographer

#endif  // CARTOGRAPHER_MAPPING_2D_SUBMAP_2D_H_
